﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Library;

namespace OA.Exam
{
    public partial class PreviewPaper : System.Web.UI.Page
    {
        Exam myExam = new Exam();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["paperId"] != null)
                {
                    string paperId = Request["paperId"].ToString();
                    HiddenPaperId.Value = paperId;
                    //试卷名称，分值，时长
                    LabPaperName.Text = myExam.GetRecordOfPaper(paperId).Rows[0]["papername"].ToString();
                    string score = myExam.GetRecordOfPaper(paperId).Rows[0]["score"].ToString();

                    if (score == "0")
                    {
                        score = "未设置";
                    }
                    LabScore.Text = score;

                    LabExamTime.Text = myExam.GetRecordOfPaper(paperId).Rows[0]["examtime"].ToString();
                    //绑定大题
                    BindBig(paperId);
                    //绑定主观题分值
                    BindSubjectScore(paperId);
                }
            }
        }

        protected void BindBig(string paperId)
        {
            string sql = "select ItemType,Score,TypeName from Exam_PapersQuestionTypes as a left join Exam_QuestionTypes as b on a.ItemType=b.TypeId where PaperId='" + paperId + "' order by DispOrder";
            DataListBigQuestion.DataSource = DataAccess.getDataTable(sql);
            DataListBigQuestion.DataBind();
        }

        protected void BindSubjectScore(string paperId)
        {
            for (int i = 0; i < DataListBigQuestion.Items.Count; i++)
            {
                string itemType = DataListBigQuestion.DataKeys[i].ToString();
                DataList DataListQuestions = DataListBigQuestion.Items[i].FindControl("DataListQuestions") as DataList;
                if (itemType == "00004" || itemType == "00005")//主观题
                {
                    for (int j = 0; j < DataListQuestions.Items.Count; j++)
                    {
                        string questionId = DataListQuestions.DataKeys[j].ToString();
                        TextBox TxtSetScore = (TextBox)DataListQuestions.Items[j].FindControl("TxtSetScore");
                        TxtSetScore.Text = myExam.GetSubjectiveScoreOfQuestion(paperId, questionId).ToString();
                    }
                }
            }
        }

        #region 绑定大题
        protected void DataListBigQuestion_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                //大题编号处理
                Label LabNum = (Label)e.Item.FindControl("LabNum");
                switch (LabNum.Text)
                {
                    case "1":
                        LabNum.Text = "一";
                        break;
                    case "2":
                        LabNum.Text = "二";
                        break;
                    case "3":
                        LabNum.Text = "三";
                        break;
                    case "4":
                        LabNum.Text = "四";
                        break;
                    case "5":
                        LabNum.Text = "五";
                        break;
                }

                //题目说明处理
                string itemType = DataListBigQuestion.DataKeys[e.Item.ItemIndex].ToString();
                Label LabDescription = (Label)e.Item.FindControl("LabDescription");
                Label LabScore = (Label)e.Item.FindControl("LabScore");
                switch (itemType)
                {
                    case "00001":
                        LabDescription.Text = "（请在每题后面选择正确还是错误。";
                        break;
                    case "00002":
                        LabDescription.Text = "（下列每题备选答案中，只有一个符合题意的正确答案。请在选项中选择。";
                        break;
                    case "00003":
                        LabDescription.Text = "（下列每题备选答案中，有两个或两个以上符合题意的正确答案。请在选项中选择。";
                        break;
                    case "00004":
                        LabDescription.Text = "（下列每题中（）代表需填空的位置，请在对应的矩形框中填写答案。";
                        LabScore.Text = "）";
                        break;
                    case "00005":
                        LabDescription.Text = "（请在每题对应的矩形框中填写答案。";
                        LabScore.Text = "）";
                        break;
                }
                //绑定小题
                DataList DataListQuestions = e.Item.FindControl("DataListQuestions") as DataList;
                DataListQuestions.DataSource = myExam.GetSmallQuestions(HiddenPaperId.Value, itemType);
                DataListQuestions.DataBind();
            }
        }
        #endregion

        #region 绑定小题
        protected void DataListQuestions_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                //string questionId = ((DataListItem)((e.Item.NamingContainer).NamingContainer)).NamingContainer.DataKeyField.ToString();
                HiddenField HfQuestionId = (HiddenField)e.Item.FindControl("HfQuestionId");
                string questionId = HfQuestionId.Value;
                HiddenField HfItemType = (HiddenField)e.Item.FindControl("HfItemType");
                Label LabSubject = (Label)e.Item.FindControl("LabSubject");
                switch (HfItemType.Value)
                {
                    case "00002":
                        DataList DataListRadio = e.Item.FindControl("DataListRadio") as DataList;
                        DataListRadio.DataSource = myExam.GetItemListOfQuestion(questionId);
                        DataListRadio.DataBind();
                        break;
                    case "00003":
                        DataList DataListMultiple = e.Item.FindControl("DataListMultiple") as DataList;
                        DataListMultiple.DataSource = myExam.GetItemListOfQuestion(questionId);
                        DataListMultiple.DataBind();
                        break;
                    case "00004":
                        
                        string[] arr = LabSubject.Text.Trim().Split('（');
                        int textNum = arr.Length - 1;
                        //HtmlGenericControl DivBlank = new HtmlGenericControl("div");
                        Panel PanelBlank = (Panel)e.Item.FindControl("PanelBlank");
                        for (int i = 0; i < textNum; i++)
                        {
                            TextBox newTextBox = new TextBox();
                            newTextBox.Attributes.Add("id", "TextBox" + i.ToString());
                            newTextBox.Attributes.Add("runat", "server");
                            newTextBox.Attributes.Add("disabled", "disabled");
                            PanelBlank.Controls.Add(newTextBox);
                        }
                        break;
                }
            }
        }
        #endregion
        //设置分值
        protected void BtnSet_Click(object sender, EventArgs e)
        {
            int i, j, score, subjectiveScore, totalScore = 0;
            string itemType = string.Empty;
            string questionId = string.Empty;
            for (i = 0; i < DataListBigQuestion.Items.Count; i++)
            {
                itemType = DataListBigQuestion.DataKeys[i].ToString();
                //客观题每题分值
                subjectiveScore = int.Parse(((HiddenField)DataListBigQuestion.Items[i].FindControl("HfScore")).Value);
                DataList DataListQuestions = DataListBigQuestion.Items[i].FindControl("DataListQuestions") as DataList;
                if (itemType == "00004" || itemType == "00005")//主观题
                {
                    for (j = 0; j < DataListQuestions.Items.Count; j++)
                    {
                        questionId = DataListQuestions.DataKeys[j].ToString();
                        score = int.Parse(((TextBox)DataListQuestions.Items[j].FindControl("TxtSetScore")).Text);
                        myExam.SetScore(HiddenPaperId.Value, questionId, score);
                        totalScore += score;//循环得到主观题分值总和
                    }
                }
                else//客观题
                {
                    totalScore += subjectiveScore * DataListQuestions.Items.Count;
                }
            }
            if (myExam.UpdateTotalScore(HiddenPaperId.Value, totalScore))
            {
                LabPrompt.Text = "设置成功";
                BtnBack.Focus();
                LabScore.Text = totalScore.ToString();
            }
        }

        protected void BtnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("SetPaperQuestions.aspx?paperId=" + HiddenPaperId.Value);
        }
    }
}
